﻿using System.Collections.Generic;
using System.Linq;
using JBeamLibrary.Tools;
using JBeamLibrary.Types;
using JBeamLibrary.VehicleParts.Collections;
using JBeamLibrary.VehicleParts.Elements;
using JBeamLibrary.VehicleParts.Interfaces;

namespace JBeamLibrary.VehicleParts.Groups
{
    public class Flexbodies : IVehicleGroup<Flexbody>
    {
        #region Fields

        private readonly Dictionary<int, Float3> _rotationList = new Dictionary<int, Float3>();
        private readonly Dictionary<int, Float3> _translationList = new Dictionary<int, Float3>();
        private readonly Dictionary<int, int> _disableSectionList = new Dictionary<int, int>();
        private readonly Dictionary<int, bool> _debugPositioningList = new Dictionary<int, bool>();
        private readonly Dictionary<int, bool> _isCollisionMeshList = new Dictionary<int, bool>();
        private readonly Dictionary<int, DeformGroup> _deformGroupList = new Dictionary<int, DeformGroup>();
        private readonly FlexbodyCollection _collection = new FlexbodyCollection();

        #endregion

        #region Indexers

        /// <summary>Gets the mesh links at the specified index</summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Flexbody this[int index]
        {
            get
            {
                if (Collection.Count == 0)
                    return null;
                if (index < 0 || index >= Collection.Count)
                    return null;
                return Collection[index];
            }
        }

        /// <summary>Gets the links for the specified mesh</summary>
        /// <param name="mesh"></param>
        /// <returns></returns>
        public Flexbody this[string mesh]
        {
            get { return Collection[mesh]; }
        }

        #endregion

        #region Properties

        public Dictionary<int, Float3> RotationList { get { return _rotationList; } }

        public Dictionary<int, Float3> TranslationList { get { return _translationList; } }

        public Dictionary<int, int> DisableSectionList { get { return _disableSectionList; } }

        public Dictionary<int, bool> DebugPositioningList { get { return _debugPositioningList; } }

        public Dictionary<int, bool> IsCollisionMeshList { get { return _isCollisionMeshList; } }

        public Dictionary<int, DeformGroup> DeformGroupList { get { return _deformGroupList; } }

        public FlexbodyCollection Collection { get { return _collection; } }

        public IVehicleGroupCollection<Flexbody> GroupCollection
        {
            get { return Collection; }
        }

        public int Count
        {
            get { return Collection.Count; }
        }

        public bool IsEmpty
        {
            get { return GroupHelper.GetPropertyLists(this).All(list => list.Count == 0) && Collection.Count == 0; }
        }

        #endregion

        #region Methods

        public void SetElementValue(int index, string propName, object value)
        {
            var elem = Collection[index];
            var prop = elem.GetType().GetProperty(propName);
            prop.SetValue(elem, value);
        }

        public object GetElementValue(int index, string propName)
        {
            var elem = Collection[index];
            var prop = elem.GetType().GetProperty(propName);
            return prop.GetValue(elem);
        }

        public IEnumerable<KeyValuePair<string, object>> GetElementValues(int index)
        {
            var elem = Collection[index];
            var props = elem.GetType().GetProperties();
            return props.Select(prop => new KeyValuePair<string, object>(prop.Name, prop.GetValue(elem))).AsEnumerable();
        }

        #endregion
    }
}
